// ValgFagDialog.cpp : implementation file
//

#include "stdafx.h"
#include "EBASApp.h"
#include "ValgFagDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CValgFagDialog dialog


CValgFagDialog::CValgFagDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CValgFagDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CValgFagDialog)
	m_slutdato = 0;
	m_startdato = 0;
	//}}AFX_DATA_INIT

	m_antalValgte = 0;
	m_valgte = NULL;
}

void CValgFagDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CValgFagDialog)
	DDX_Control(pDX, IDC_LIST_FAG, m_fag);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER_SLUTDATO, m_slutdato);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER_STARTDATO, m_startdato);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CValgFagDialog, CDialog)
	//{{AFX_MSG_MAP(CValgFagDialog)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CValgFagDialog message handlers

BOOL CValgFagDialog::OnInitDialog()
{
	BOOL res = CDialog::OnInitDialog();

	try {
		CWnd * pWnd = NULL;
		pWnd = (CWnd*)GetDlgItem(IDC_DATETIMEPICKER_STARTDATO);
		pWnd->EnableWindow(FALSE);
		pWnd = (CWnd*)GetDlgItem(IDC_DATETIMEPICKER_SLUTDATO);
		pWnd->EnableWindow(FALSE);

		IUnknown * pUnk = NULL; ULONG fetched = 0;	
		EBASDLLLib::IEnumFagPtr pEnumFag = NULL;
		pEnumFag.CreateInstance(__uuidof(EBASDLLLib::EnumFag));
		pEnumFag->Next(&pUnk, &fetched);
		while (NULL != pUnk) {
			EBASDLLLib::IFagPtr pFag = NULL;
			pUnk->QueryInterface(__uuidof(EBASDLLLib::IFag), (void**)&pFag);
			pUnk = NULL;

			m_fag.AddString(CString((char*)pFag->Navn));

			pFag = NULL;

			pEnumFag->Next(&pUnk, &fetched);
		}

	} catch (...) {

	}

	UpdateData(FALSE);

	return res;
}

void CValgFagDialog::OnOK() 
{
	m_antalValgte = m_fag.GetSelCount();
	int * rgSel = new int[m_antalValgte];
	m_fag.GetSelItems(m_antalValgte, rgSel);
	m_valgte = new CString[m_antalValgte];

	for (int i = 0; i < m_antalValgte; i++) {
		CString str;
		m_fag.GetText(rgSel[i], str);
		m_valgte[i] = str;
	}
	
	CDialog::OnOK();
}
